﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SubSystem.Models.ObjectStore
{
    public class TestObjectStore<TStoreable> : IObjectStore<TStoreable> where TStoreable : IStoreable
    {
        public TestObjectStore(ref List<TStoreable> list)
        {
            Items = list; 
        }

        protected List<TStoreable> Items { get; set; }

        public IEnumerable<TStoreable> Fetch()
        {
            return Items.AsEnumerable();
        }

        public bool Save(TStoreable item)
        {
            try
            {
                var toRemove = Items.Where(i => i.Id == item .Id);
                if (toRemove.Count() > 0)
                {
                    Items.RemoveAll(i => i.Id == item .Id);
                }
                Items.Add(item);
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }
    }
}